﻿
using System;
using System.ComponentModel;
using System.Linq;
using System.Windows.Forms;

namespace ResourceReportBuilder
{
    public class PathLink : LinkLabel
    {
        [Browsable(false)]
        public new string Text
        {
            get
            {
                return GetText(Path);
            }
            set
            {
                base.Text = GetText(value);
            }
        }

        private string _path;
        public string Path
        {
            get { return _path; }
            set
            {
                _path = value;
                base.Text = GetText(_path);
            }
        }

        private string _textFormat;
        public string TextFormat
        {
            get { return _textFormat; }
            set { _textFormat = value; }
        }

        public PathLink()
        {
        }

        [Browsable(false)]
        public override bool AutoSize
        {
            get
            {
                return base.AutoSize;
            }
            set
            {
                base.AutoSize = false;
            }
        }

        private string GetText(string value)
        {
            if (this.AutoSize)
            {
                return value;
            }
            System.Drawing.Size sz = TextRenderer.MeasureText(value, this.Font);
            if (sz.Width + 2 <= this.Width)
            {
                return value;
            }
            string[] dirs = value.Split('\\');
            if (dirs.Length < 2)
            {
                return value;
            }
            string result = string.Empty;
            string oldResult = string.Empty;
            string rightVal = string.Empty;
            string leftVal = string.Empty;
            int separatorWidth = TextRenderer.MeasureText("...\\", this.Font).Width + 2;
            for (int i = 0; i < dirs.Length / 2; i++)
            {
                oldResult = rightVal;
                rightVal = System.IO.Path.Combine(dirs.ToList().Reverse<string>().Where((item, index) => index <= i).Reverse<string>().ToArray());
                if (TextRenderer.MeasureText(rightVal, this.Font).Width + separatorWidth >= Width)
                {
                    return oldResult;
                }
                rightVal = System.IO.Path.Combine("...", rightVal);
                leftVal = string.IsNullOrWhiteSpace(leftVal) ? dirs[i] : string.Format("{0}\\{1}", leftVal, dirs[i]); 
                oldResult = result;
                result = string.Format("{0}\\{1}", leftVal, rightVal);
                if (TextRenderer.MeasureText(result, this.Font).Width + 2 >= Width)
                {
                    return oldResult;
                }
            }
            return value;
        }

        protected override void OnResize(EventArgs e)
        {
            base.Text = GetText(_path);
            base.OnResize(e);
        }

        protected override void OnSizeChanged(EventArgs e)
        {
            base.Text = GetText(_path);
            base.OnSizeChanged(e);
        }
    }
}
